layui.use(['table', 'jquery', 'element', 'layer', 'form', 'upload'], function () {
    var table = layui.table;
    var $ = layui.jquery;
    var element = layui.element;
    var layer = layui.layer;
    var form = layui.form;
    var upload = layui.upload;


    upload.render({
        elem: '#fileupload'
        , url: '',
        field: 'file',
        auto: false
    })

    //第一个实例
    table.render({
        elem: '#table',
        height: 600,
        toolbar: '#toolbar',
        defaultToolbar: []
        , url: ctx + '/eng/system/banner/listPage' //数据接口
        , page: true
        , cols: [[
            {
                field: 'url', title: '图片', align: 'center', width: '', templet: function (d) {
                    return "<img src='" + ctx + "/images" + d.url + "' style='height: 100px;width: 100px;'>";
                }
            }
            , {field: 'name', title: '轮播图名称', align: 'center', width: ''}
            , {
                field: 'type', title: '类型名称', align: 'center', width: '', templet: function (d) {
                    if (d.type == 1) {
                        return "废品回收轮播图";
                    }
                    return "积分商城轮播图";
                }
            }
            , {field: 'seq', title: '轮播顺序', align: 'center', width: ''}
            , {field: 'createTime', title: '创建时间', align: 'center', width: ''}
            , {field: 'createName', title: '创建人', align: 'center', width: ''}
            , {
                field: 'status', title: '状态', align: 'center', width: '', templet: function (d) {
                    if (d.status == 1) {
                        return "上线";
                    }
                    return "下线";
                }
            }
            , {field: 'ops', title: '操作', width: '', align: 'center', toolbar: '#tool'}
        ]]
    });


    // tool
    table.on('tool(table)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'up') {
            changeStatus('up', data);
        } else if (layEvent === 'down') {
            changeStatus('down', data);
        }
    });

    //toolbar
    table.on('toolbar(table)', function (obj) {
        var event = obj.event;
        if (event == 'addBanner') {
            // 新增商品信息
            layer.open({
                type: 1,
                content: $('#addLayer')
            });
        }
    });

    // 新增提交
    $('#submit').on('click', function (event) {
        event.preventDefault();
        var formData = new FormData();
        formData.append("name", $("#addForm input[name='name']").val());
        formData.append("type", $("#addForm select[name='type']").val());
        formData.append("seq", $("#addForm input[name='seq']").val());
        formData.append("file", $("input[name='file']")[0].files[0]);
        $.ajax({
            url: '/eng/system/banner/add',
            type: 'post',
            data: formData,
            contentType: false,
            processData: false,
            success: function (res) {
                if (res.success) {
                    layer.closeAll();
                    layer.msg(res.msg, {icon: 1});
                    table.reload('table');
                    $("#reset").click();
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }
        })
    });

    function changeStatus(flag, data) {
        var status = null;
        if (flag == 'up') {
            status = 1;
        } else {
            status = 0;
        }
        $.ajax({
            url: ctx + '/eng/system/banner/update',
            data: {id: data.id, status: status},
            success: function (res) {
                if (res.success) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('table');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }
        });
    }
});